﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PhongKham.Class;

namespace PhongKham.Controller
{
    public class ClsDmDienMienGiam
    {
        SqlCommand SCommand;
        SqlCommand ICommand;
        SqlCommand UCommand;
        SqlCommand DCommand;

        public DataSet dsDanhMuc;
        private SqlDataAdapter daDanhMuc;
        private ClsBaoLoi objBaoLoi = new ClsBaoLoi();

        public ClsDmDienMienGiam() 
        {
            dsDanhMuc = new DataSet();

            daDanhMuc = new SqlDataAdapter();
            SCommand = new SqlCommand();
            ICommand = new SqlCommand();
            UCommand = new SqlCommand();
            DCommand = new SqlCommand();

            //select
            SCommand.CommandText = "BN_DM_DIEN_MIENGIAM_SELECT";
            SCommand.CommandType = CommandType.StoredProcedure;
            SCommand.Connection = ClsConnection.Conn;

            //insert
            ICommand.CommandText = "BN_DM_DIEN_MIENGIAM_INSERT";
            ICommand.CommandType = CommandType.StoredProcedure;
            ICommand.Connection = ClsConnection.Conn;
            ICommand.Parameters.Add(new SqlParameter("@DM_DIEN_MG_STT", SqlDbType.BigInt, 8, "DM_DIEN_MG_STT"));
            ICommand.Parameters.Add(new SqlParameter("@DM_DIEN_MG_TEN", SqlDbType.NVarChar, 255, "DM_DIEN_MG_TEN"));
            ICommand.Parameters.Add(new SqlParameter("@DM_DIEN_MG_DGIAI", SqlDbType.NVarChar, 255, "DM_DIEN_MG_DGIAI"));

            //update
            UCommand.CommandText = "BN_DM_DIEN_MIENGIAM_UPDATE";
            UCommand.CommandType = CommandType.StoredProcedure;
            UCommand.Connection = ClsConnection.Conn;
            UCommand.Parameters.Add(new SqlParameter("@DM_DIEN_MG_STT", System.Data.SqlDbType.BigInt, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "DM_DIEN_MG_STT", System.Data.DataRowVersion.Original, null));
            UCommand.Parameters.Add(new SqlParameter("@DM_DIEN_MG_TEN", System.Data.SqlDbType.NVarChar, 255, "DM_DIEN_MG_TEN"));
            UCommand.Parameters.Add(new SqlParameter("@DM_DIEN_MG_DGIAI", System.Data.SqlDbType.NVarChar, 255, "DM_DIEN_MG_DGIAI"));

            //delete
            DCommand.CommandText = "BN_DM_DIEN_MIENGIAM_DELETE";
            DCommand.CommandType = CommandType.StoredProcedure;
            DCommand.Connection = ClsConnection.Conn;
            DCommand.Parameters.Add(new SqlParameter("@DM_DIEN_MG_STT", System.Data.SqlDbType.BigInt, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "DM_DIEN_MG_STT", System.Data.DataRowVersion.Original, null));

            daDanhMuc.SelectCommand = SCommand;
            daDanhMuc.InsertCommand = ICommand;
            daDanhMuc.DeleteCommand = DCommand;
            daDanhMuc.UpdateCommand = UCommand;
        }


        public void GetDmDienMienGiam() 
        {
            dsDanhMuc.Clear();
            daDanhMuc.Fill(dsDanhMuc, "DmDienMienGiam");
        }

        public bool UpdateDmDienMienGiam() 
        {
            try 
            {
                daDanhMuc.Update(dsDanhMuc, "DmDienMienGiam");
                return true;
            }
            catch (Exception ex)
            {
                if (ex.ToString().IndexOf("Violation of PRIMARY KEY") != -1)
                    objBaoLoi.CanhBao("Số thứ tự diện miễn giảm bị trùng.\nVui lòng kiểm tra lại.");
                if (ex.ToString().IndexOf("conflicted with the REFERENCE constraint") != -1)
                    objBaoLoi.CanhBao("Diện chi phí này đang được sử dụng.\nKhông thể xóa!");
                if (ex.ToString().IndexOf("Cannot insert the value NULL") != -1)
                    objBaoLoi.CanhBao("Vui lòng nhập đầy đủ thông tin cho diện miễn giảm!");
                else
                    objBaoLoi.CanhBao(ex.Message);
                    //new HIS.Classes.ClsException(ex);
                return false;
            }
        }
    }
}
